package com.bocai.user.api;

import java.io.IOException;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.bocai.Response;
import com.bocai.ResponseCode;
import com.bocai.user.service.UserService;

/**
 * 用户接口
 * TODO（使用Session作为用户的登录凭证，是否存在安全性问题？？？）
 * @author VIC
 *
 */
@RestController
@RequestMapping("/user")
public class UserApi {
	private static final String SESSION_KEY = "LOGIN_SESSION";
	private static final int SESSION_EXIPRE = 12*60*60;	// session 过期时间（秒）
	
	@Autowired
	private UserService userService;
	
	/**
	 * 注册（Post）
	 * @param mobile
	 * @param passwd
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@PostMapping("/register")
	public Response userRegister(@RequestParam("mobile")String mobile, @RequestParam("passwd")String passwd,
			HttpServletResponse response) throws IOException {
		// 检测该号码是否已注册
		try {
			if(userService.checkMobileExisted(mobile)) {
				return new Response(ResponseCode.REGISTERED.getValue(), ResponseCode.REGISTERED.getDesc());
			}
		} catch (Exception e) {
			return new Response(ResponseCode.CHECK_MOBILE_ERROR.getValue(), ResponseCode.CHECK_MOBILE_ERROR.getDesc());
		}
		// 注册
		try {
			if(userService.register(mobile, passwd)) {
				return new Response(ResponseCode.SUCCESS.getValue(), ResponseCode.SUCCESS.getDesc());
			}else {
				return new Response(ResponseCode.REGISTER_FAILURE.getValue(), ResponseCode.REGISTER_FAILURE.getDesc());
			}
		} catch (Exception e) {
			return new Response(ResponseCode.REGISTER_FAILURE.getValue(), ResponseCode.REGISTER_FAILURE.getDesc());
		}
	}
	
	/**
	 * 登录（Post）
	 * @param mobile
	 * @param passwd
	 * @param request
	 * @param response
	 * @return
	 */
	@PostMapping("/login")
	public Response userLogin(@RequestParam("mobile")String mobile, @RequestParam("passwd")String passwd,
			HttpServletRequest request, HttpServletResponse response) {
		try {
			if(userService.checkUserLogin(mobile, passwd)) {
				// 设置当前用户登录session
				HttpSession session = request.getSession();
				session.setAttribute(SESSION_KEY, mobile);
				session.setMaxInactiveInterval(SESSION_EXIPRE);
				return new Response(ResponseCode.SUCCESS.getValue(), ResponseCode.SUCCESS.getDesc());
			}else {
				return new Response(ResponseCode.LOGIN_FAILED.getValue(), ResponseCode.LOGIN_FAILED.getDesc());
			}
		} catch (Exception e) {
			return new Response(ResponseCode.LOGIN_ERROR.getValue(), ResponseCode.LOGIN_ERROR.getDesc());
		}
	}
	
	/**
	 * 登出、退出登录（Post）
	 * @param request
	 * @param response
	 * @return
	 */
	@PostMapping("/logout")
	public Response userLogout(HttpServletRequest request, HttpServletResponse response) {
		HttpSession session = request.getSession(false);
		if(null != session) {
			session.invalidate();
		}
		return new Response(ResponseCode.SUCCESS.getValue(), ResponseCode.SUCCESS.getDesc());
	}
	
	/**
	 * 获取当前登录用户的信息数据
	 * @param request
	 * @param response
	 * @return
	 */
	@GetMapping("/userInfo")
	public Response userInfo(HttpServletRequest request, HttpServletResponse response) {
		try {
			HttpSession session = request.getSession(false);
			if(null != session) {
				String mobile = (String)session.getAttribute(SESSION_KEY);
				return new Response(userService.getUserInfoByMobile(mobile));
			}
			return new Response(ResponseCode.NOT_LOGIN.getValue(), ResponseCode.NOT_LOGIN.getDesc());
		} catch (Exception e) {
			return new Response(ResponseCode.SERVER_ERROR.getValue(), ResponseCode.SERVER_ERROR.getDesc());
		}
	}
	
	/**
	 * 增加/修改用户银行信息（Post）
	 * @param mobile
	 * @param bankname
	 * @param bankno
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@PostMapping("/updatebankinfo")
	public Response updateBankInfo(@RequestParam("mobile")String mobile,
			@RequestParam("bankname")String bankname, @RequestParam("bankno")String bankno, 
			HttpServletResponse response) throws IOException {		
		try {			
			if(userService.updatebankinfo(mobile, bankname, bankno)) {
				return new Response(ResponseCode.SUCCESS.getValue(), ResponseCode.SUCCESS.getDesc());
			}else {
				return new Response(ResponseCode.REGISTER_FAILURE.getValue(), ResponseCode.REGISTER_FAILURE.getDesc());
			}
		} catch (Exception e) {
			return new Response(ResponseCode.REGISTER_FAILURE.getValue(), ResponseCode.REGISTER_FAILURE.getDesc());
		}
	}
	
	/**
	 * 修改密码（Post）
	 * @param mobile
	 * @param password
	 * @param newpassword
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@PostMapping("/updatepassword")
	public Response updatePassword(@RequestParam("mobile")String mobile,
			@RequestParam("password")String password, @RequestParam("newpassword")String newpassword, 
			HttpServletResponse response) throws IOException {		
		try {			
			if(userService.updatePassword(mobile, password, newpassword)) {
				return new Response(ResponseCode.SUCCESS.getValue(), ResponseCode.SUCCESS.getDesc());
			}else {
				return new Response(ResponseCode.REGISTER_FAILURE.getValue(), ResponseCode.REGISTER_FAILURE.getDesc());
			}
		} catch (Exception e) {
			return new Response(ResponseCode.REGISTER_FAILURE.getValue(), ResponseCode.REGISTER_FAILURE.getDesc());
		}
	}
}
